package com.beardedhen.harmonybootstrap;

import com.beardedhen.harmonybootstrap.api.attributes.BootstrapBrand;
import com.beardedhen.harmonybootstrap.api.attributes.BootstrapHeading;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapBrand;
import com.beardedhen.harmonybootstrap.api.defaults.DefaultBootstrapHeading;
import com.beardedhen.harmonybootstrap.api.view.BootstrapBrandView;
import com.beardedhen.harmonybootstrap.api.view.BootstrapHeadingView;
import com.beardedhen.harmonybootstrap.api.view.RoundableView;
import com.beardedhen.harmonybootstrap.utils.UiUtil;
import com.beardedhen.harmonybootstrap.utils.ViewUtils;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import ohos.agp.components.element.Element;
import ohos.agp.render.Canvas;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * BootstrapLabels are designed for showing text styled with BootstrapBrands - they should be
 * considered similar to a BootstrapButton, but without the press functionality. It is possible to
 * set the size of BootstrapLabels using H1-H6 elements.
 */
public class BootstrapLabel extends Text implements Component.DrawTask, RoundableView, BootstrapHeadingView, BootstrapBrandView {

    private static final String BOOTSTRAP_LABEL_BOOTSTRAP_HEADING = "bootstrapHeading";
    private static final String BOOTSTRAP_LABEL_ROUNDED_CORNERS = "roundedCorners";
    private static final String BOOTSTRAP_LABEL_BOOTSTRAP_BRAND = "bootstrapBrand";
    private BootstrapHeading bootstrapHeading;
    private BootstrapBrand bootstrapBrand;
    private boolean rounded;
    private float oldheight,newheight;

    public BootstrapLabel(Context context) throws NotExistException, WrongTypeException, IOException {
        super(context);
        initialise(null);
    }

    public BootstrapLabel(Context context, AttrSet attrs) throws NotExistException, WrongTypeException, IOException {
        super(context, attrs);
        initialise(attrs);
    }

    public BootstrapLabel(Context context, AttrSet attrs, String defStyle) throws NotExistException, WrongTypeException, IOException {
        super(context, attrs, defStyle);
        initialise(attrs);
    }

    private void initialise(AttrSet attrs) throws NotExistException, WrongTypeException, IOException {
        if (attrs == null) {
            return;
        }
        addDrawTask(this::onDraw);
        try {

            this.rounded = attrs.getAttr(BOOTSTRAP_LABEL_ROUNDED_CORNERS).isPresent() ? attrs.getAttr(
                BOOTSTRAP_LABEL_ROUNDED_CORNERS).get().getBoolValue() : false;

            int typeOrdinal = -1;
            if (attrs.getAttr(BOOTSTRAP_LABEL_BOOTSTRAP_BRAND).isPresent()) {
                String typeOrdinalStr = attrs.getAttr(BOOTSTRAP_LABEL_BOOTSTRAP_BRAND).get().getStringValue().trim();
                typeOrdinal = UiUtil.getBootstrapBrandInt(typeOrdinalStr);
            }

            int attrValue = -1;
            if (attrs.getAttr(BOOTSTRAP_LABEL_BOOTSTRAP_HEADING).isPresent()) {
                String attrValueStr = attrs.getAttr(BOOTSTRAP_LABEL_BOOTSTRAP_HEADING).get().getStringValue().trim();
                attrValue = UiUtil.getBootstrapHeadingInt(attrValueStr);
            }

            this.bootstrapHeading = DefaultBootstrapHeading.fromAttributeValue(attrValue);
            this.bootstrapBrand = DefaultBootstrapBrand.fromAttributeValue(typeOrdinal);

        } catch (Exception e){
            e.printStackTrace();
        }
        updateBootstrapState();
    }

     private void updateBootstrapState() throws NotExistException, WrongTypeException, IOException {

        if (bootstrapHeading != null) {
            int vert = (int) bootstrapHeading.verticalPadding(getContext());
            int hori = (int) bootstrapHeading.horizontalPadding(getContext());

            setPadding(hori, vert, hori, vert);
            setTextSize((int) bootstrapHeading.getTextSize(getContext()));
        }
        setTextColor(Color.WHITE);

         oldheight = getHeight();

        Element bg = BootstrapDrawableFactory.bootstrapLabel(
                getContext(),
                bootstrapBrand,
                rounded,
                oldheight);

       ViewUtils.setBackgroundDrawable(this,bg);
    }


    @Override
    public void onDraw(Component component, Canvas canvas) {

        newheight = component.getHeight();

        if(rounded && newheight != oldheight){
            try {
                updateBootstrapState();
            } catch (NotExistException e) {
                e.printStackTrace();
            } catch (WrongTypeException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void setBootstrapBrand(BootstrapBrand bootstrapBrand) throws NotExistException, WrongTypeException, IOException {
        this.bootstrapBrand = bootstrapBrand;
        updateBootstrapState();
    }

    @Override
    public BootstrapBrand getBootstrapBrand() {
        return bootstrapBrand;
    }

    @Override
    public void setRounded(boolean rounded) throws NotExistException, WrongTypeException, IOException {
        this.rounded = rounded;
        updateBootstrapState();
    }

    @Override
    public boolean isRounded() throws NotExistException, WrongTypeException, IOException {
        return rounded;
    }

    @Override
    public void setBootstrapHeading(BootstrapHeading bootstrapHeading) throws NotExistException, WrongTypeException, IOException {
        this.bootstrapHeading = bootstrapHeading;
        updateBootstrapState();
    }

    @Override
    public BootstrapHeading getBootstrapHeading() {
        return bootstrapHeading;
    }
}
